'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { INTEGER, STRING, DATE, TINYINT, TEXT } = app.Sequelize;
    const Announcement = app.model.define(
        'Announcement',
        {
            id: { type: INTEGER, primaryKey: true, autoIncrement: true },
            title: { type: STRING(100), allowNull: false, comment: '公告标题' },
            content: { type: TEXT, allowNull: false, comment: '公告内容' },
            publisher: { type: STRING(50), allowNull: true, comment: '发布人' },
            publish_time: { type: DATE, allowNull: true, comment: '发布时间' },
            is_top: { type: TINYINT(1), defaultValue: 0, comment: '是否置顶：0-否，1-是' },
            status: { type: TINYINT(1), defaultValue: 1, comment: '状态：0-禁用，1-启用' },
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            update_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'announcement',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    return Announcement;
};
